package problem_everyday;

import java.util.HashMap;
import java.util.Map;

class FindSumPairs {
    private int[] nums1, nums2;
    private Map<Integer, Integer> map;

    public FindSumPairs(int[] nums1, int[] nums2) {
        this.nums1 = nums1;
        this.nums2 = nums2;
        this.map = new HashMap<>(nums2.length);
        for(int i=0; i<nums2.length; i++){
            int num = nums2[i];
            if(map.containsKey(num)){
                map.put(num, map.get(num) + 1);
            } else {
                map.put(num, 1);
            }
        }
    }

    public void add(int index, int val) {
        int num = this.nums2[index];
        int count = map.get(num);
        if(count==1){
            map.remove(count);
        } else {
            map.put(num, count-1);
        }
        int new_num = num + val;
        if(map.containsKey(new_num)){
            map.put(new_num, map.get(new_num)+1);
        } else{
            map.put(new_num, 1);
        }
        this.nums2[index] = new_num;
    }

    public int count(int tot) {
        int res = 0;
        for(int num: this.nums1){
            if(tot<num) continue;
            if(map.containsKey(tot-num)){
                res += map.get(tot-num);
            }
        }
        return res;
    }
}

public class D2025_07_06_P1865 {
    public static void main(String[] args) {
        int[] nums1 = {1,1,2,2,3,3};
        int[] nums2 = {1,4,5,2,5,4};
        FindSumPairs findSumPairs = new FindSumPairs(nums1, nums2);
        System.out.println(findSumPairs.count(7));
        findSumPairs.add(3,2);
        System.out.println(findSumPairs.count(8));
        System.out.println(findSumPairs.count(4));
        findSumPairs.add(0,1);
        findSumPairs.add(1,1);
        System.out.println(findSumPairs.count(7));

    }
}
